%option noyywrap
%option yylineno

%{
#include<bits/stdc++.h>
#include <stdio.h>
#include "sysy.tab.h"
#include "def.h"
using namespace std;
int yycolumn=1;
#define YY_USER_ACTION    	yylloc.first_line=yylloc.last_line=yylineno; \
	yylloc.first_column=yycolumn;	yylloc.last_column=yycolumn+yyleng-1; yycolumn+=yyleng;
#define YYSTYPE YYLVAL
%}
IGNORE      (\/\/[^\n]*\n)|(\/\*[^\*]*\*\/)|([ \f\n\r\t\v])
WHILE       while
INT         int
FLOAT       float
VOID        void
CONST       const
RETURN      return
IF          if
ELSE        else
FOR         for
DO          do
BREAK       break
CONTINUE    continue
LP          \(
RP          \)
LC          \{
RC          \}
LB	    \[
RB	    \]
INCREASE    \+\+
DECREASE    \-\-
ADD         \+
MINUS       \-
MUL         \*
MOD	    %
EQ          ==
GE          \>=
LE          \<=
GT          \>
LT          \<
NOT         !
ASSIGN      =
COMMA       ,
SEMICOLON   ;
FLOAT_VAL   ([0-9]+e[-]?[0-9]+f)|(([\-]?[1-9][0-9]*|0)\.[0-9]+f?)
INT_VAL     ([\-]?[1-9][0-9]*|0)|(0x([1-9a-f][0-9a-f]*|0))|(0([1-7][0-7]*|0))
ID          [A-Za-z_][A-Za-z0-9_]*
ERROR_OCT   0[0-7]*[8-9][0-9a-f]*
ERROR_DEC   [1-9][0-9]*[a-f][0-9a-f]*
ERROR_ID    [0-9]+[0-9]*l[0-9]*

%%
{IGNORE} 	{}
{ERROR_OCT}  	{printf("Lexical error - line %d : %s\n",yylineno , yytext); return LEX_ERR; }
{ERROR_DEC}  	{printf("Lexical error - line %d : %s\n",yylineno , yytext); return LEX_ERR; }
{ERROR_ID}  	{printf("Lexical error - line %d : %s\n",yylineno , yytext); return LEX_ERR; }
{WHILE} 	{ return WHILE; }
{INT} {
	yylval.ptr=new node(0,SYM_TYPE_INT); 
	yylval.ptr->type_val=new string("Btype"); 
	yylval.ptr->analyze();
	return INT; 
      }
{FLOAT} {
	yylval.ptr=new node(0,SYM_TYPE_FLOAT); 
	yylval.ptr->type_val=new string("Btype"); 
	yylval.ptr->analyze();
	return FLOAT; 
     }
{VOID} {
	yylval.ptr=new node(0,SYM_TYPE_VOID); 
	yylval.ptr->type_val=new string("Btype"); 
	yylval.ptr->analyze();
	return VOID; 
     }
{CONST} 	{ return CONST; }
{RETURN} 	{ return RETURN; }
{IF} 		{ return IF; }
{ELSE} 	{ return ELSE; }
{FOR} 		{ return FOR; }
{DO} 		{ return DO; }
{BREAK} 	{ return BREAK; }
{CONTINUE} 	{ return CONTINUE; }
{COMMA} 	{ return COMMA; }
{SEMICOLON} 	{ return SEMICOLON; }
{LP} 		{ return LP; }
{RP} 		{ return RP; }
{LB} 		{ return LB; }
{RB} 		{ return RB; }
{LC} 		{ return LC; }
{RC} 		{ return RC; }
{GT} 		{ return GT; }
{EQ} 		{ return EQ; }
{GE} 		{ return GE; }
{LE} 		{ return LE; }
{LT} 		{ return LT; }
{NOT}		{ return NOT;}
"/"  		{ return DIV; }
"&&" 		{ return AND; }
"||" 		{ return OR; }
{INCREASE} 	{ return INCREASE; }
{DECREASE} 	{ return DECREASE; }
{ADD} 		{ return ADD; }
{MINUS} 	{ return MINUS; }
{MUL} 		{ return MUL; }
{MOD} 		{ return MOD; }
{ASSIGN} 	{ return ASSIGN; }
{FLOAT_VAL} {
	yylval.ptr=new node(2,SYM_FLOAT);
	yylval.ptr->float_val=atof(yytext);
	yylval.ptr->lineno=yylineno; 
	yylval.ptr->analyze();
	return FLOAT_VAL;
      }
{INT_VAL} {
	yylval.ptr=new node(1,SYM_INT);
	yylval.ptr->int_val=atoi(yytext);
	yylval.ptr->lineno=yylineno; 
	yylval.ptr->analyze();
	return INT_VAL;
      }
{ID} {
	yylval.ptr=new node(6,SYM_ID);
	yylval.ptr->type_val=new string(yytext);
	yylval.ptr->lineno=yylineno; 
	yylval.ptr->analyze();
	return ID; 
      }

%%



